package example.springboot.controller;


import example.springboot.mapper.userMapper;
import example.springboot.model.Question;
import example.springboot.model.t_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;



@Controller
public class publishController {
    @Autowired
    private example.springboot.mapper.questionMapper questionMapper;
    @Autowired
    private Question question;
    @Autowired
    private userMapper userMapper;
    //点击提问跳转到发布页面
    @GetMapping("/publish")
    public  String publiseController(){
        return "publish";
    }

    //编辑
    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") String id,
                       Model model) {
        Question question = questionMapper.byIdFind(id);
        model.addAttribute("title", question.getTitle());
        model.addAttribute("description", question.getDescription());
        model.addAttribute("tag", question.getTag());
        model.addAttribute("id", question.getId());
       // model.addAttribute("tags", TagCache.get());
        return "publish";
    }
    //点击发布
    @PostMapping("/publish")
    public String questionController(@RequestParam(value = "title",required = false) String title,
                                     @RequestParam(value = "description",required = false) String description,
                                     @RequestParam(value = "tag",required = false) String tag,
                                     HttpServletRequest request,
                                     Model model){
        if (title==null||title==""){

            model.addAttribute("error","请输入标题");
            return "publish";
        }else {
            model.addAttribute("description",description);
            model.addAttribute("tag",tag);
        }
        if (description==null||description==""){
            model.addAttribute("error","请输入问题补充");
            return "publish";
        }else {
            model.addAttribute("title", title);
            model.addAttribute("tag",tag);
        }
        if (tag==null||tag==""){
            model.addAttribute("error","请添加标签");
            return "publish";
        }else {
            model.addAttribute("title", title);
            model.addAttribute("description",description);
        }

        t_user user1 = (t_user) request.getSession().getAttribute("t_user");
        if (user1 == null) {
            model.addAttribute("error", "用户未登录");
            return "publish";
        }
        question.setDescription(description);
        question.setTitle(title);
        question.setTag(tag);
        t_user user = (t_user) request.getSession().getAttribute("t_user");
        question.setCreator((user.getAccountid()));
        question.setGmt_create(System.currentTimeMillis());
        question.setGmt_modified(user.getGmtmodified());
        questionMapper.insert(question);
        //存入id，用户列表显示回传id
        model.addAttribute("id",question.getCreator());
        model.addAttribute("question",question);
        return "/";
    }

}
